//
//  SecondViewController.m
//  TestNavigation
//
//  Created by xuzhiming on 16/11/30.
//  Copyright © 2016年 xuzhiminglongma. All rights reserved.
//

#import "SecondViewController.h"

#define kTableHeaderViewHeight 200

@interface SecondViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong)   UILabel *titleLabe;
@end

@implementation SecondViewController

-(void)viewDidLoad{
    [super viewDidLoad];
    [self setupTableView];
    self.edgesForExtendedLayout = UIRectEdgeAll;
    [self setupTableViewTitle];
    // 初始透明
    UIView *navBackgroundView = [self.navigationController.navigationBar subviews][0];
    navBackgroundView.alpha = 0.0;
}

-(void)setupTableView{
    _tableView = [[UITableView alloc] initWithFrame:self.view.bounds];
    _tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), kTableHeaderViewHeight)];
    headerView.backgroundColor = [UIColor greenColor];
    _tableView.tableHeaderView = headerView;
    [self.view addSubview:_tableView];
}

-(void)setupTableViewTitle{
    _titleLabe = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, CGRectGetWidth(self.view.frame), 20)];
    [_titleLabe setText:@"我是标题"];
    [_titleLabe setTextAlignment:NSTextAlignmentCenter];
    [_titleLabe setBackgroundColor:[UIColor redColor]];
    [_titleLabe setTextColor:[UIColor whiteColor]];
    [self.view addSubview:_titleLabe];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 100;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 44;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString * identify = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identify];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identify];
    }
    int R = (arc4random() % 256) ;
    int G = (arc4random() % 256) ;
    int B = (arc4random() % 256) ;
    [cell setBackgroundColor:[UIColor colorWithRed:R/255.0 green:G/255.0 blue:B/255.0 alpha:1]];
    return cell;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    CGFloat offset = scrollView.contentOffset.y;
    CGFloat alpha = 1.0;
    if (offset < kTableHeaderViewHeight - 64) {
        alpha = offset / 100.0;
    }
    else if(offset < 0){
        alpha = 0.0;
    }
    else{
        alpha = 1.0;
    }
    UIView *navBackgroundView = [self.navigationController.navigationBar subviews][0];
    navBackgroundView.alpha = alpha;
    
    // 设置标题位置
    CGFloat title_y = 0;
    if (offset >= kTableHeaderViewHeight - 20 - 64) {
        title_y = 64;
    }
    else{
        title_y = kTableHeaderViewHeight - 20 - offset;
    }
    [_titleLabe setFrame:CGRectMake(0, title_y, CGRectGetWidth(self.view.frame), 20)];
}
@end
